<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2017/6/5
 * Time: 上午9:38
 */

namespace app\api\controller;

use app\common\controller\ActionLogs;
use app\common\controller\Platinfo;
use app\common\model\Agent;
use app\common\model\Merchant;
use think\Config;

class Api
{
    public function api()
    {
        try {
//            $data['return_code'] = "FAIL";
//            $data['result_code'] = "FAIL";
//            $data['result_msg'] = "系统升级中请稍后再试";
//            $data['return_msg'] = "系统升级中请稍后再试";
//            return json($data);
//            return json_encode($data);
            $postData = file_get_contents('php://input');
            if ($postData) {
                $encode = mb_detect_encoding($postData, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
                $postData = mb_convert_encoding($postData, "UTF-8", $encode);

                if (is_json($postData)) {
                    $param1 = json_decode($postData, true);
                    $query_type = $param1['method'];
                    $param = json_decode($postData, true);
                    trace( "API请求数据" . json_encode($param,256),'SysRunLog');
                    $check = $this->checkparam(array_keys($param), $query_type);
                    if (isset($param['merchantid'])) {
                        $s = $this->checkappid($param['appid'], $param['merchantid']);
                        if ($s == "FAIL") {
                            $data['return_code'] = "FAIL";
                            $data['result_code'] = "FAIL";
                            $data['result_msg'] = "appid与merchantid不匹配";
                            $data['return_msg'] = "appid与merchantid不匹配";
                            trace( "API返回结果" . json_encode($data,256),'SysRunLog');
                            return json_encode($data);
                        }

                        $res_chack_agent = $this->check_agent($param);
                        if ($res_chack_agent['code'] == "FAIL") {
                            $data['return_code'] = "FAIL";
                            $data['result_code'] = "FAIL";
                            $data['result_msg'] = "已到期";
                            $data['return_msg'] = "已到期";
                            trace( "API返回结果" . json_encode($data,256),'SysRunLog');
                            return json_encode($data);
                        }
                    }

                    if ($check['code'] == "0") {
                        unset($param1['sign']);
                        $sign = getsign(uni_array($param1), "MD5", $param['appid']);
//                        if ($param['merchantid']=='501'){
//                            $CardsThree = new CardsThree();
//                            $sign = $CardsThree->getsign(uni_array($param1), "MD5", $param['appid']);
//                        }else{
//                            $sign = getsign(uni_array($param1), "MD5", $param['appid']);
//                        }
                        if ($sign == $param['sign'] || $param['method'] == "apply_agent") {
                            $data = $this->api_query($param);

                        } else {
                            $data['return_code'] = "SUCCESS";
                            $data['result_code'] = "FAIL";
                            $data['result_msg'] = "签名错误";
                            $data['return_msg'] = "签名错误" . $sign;
                        }
                    } else {
                        $data['return_code'] = "SUCCESS";
                        $data['result_code'] = "FAIL";
                        $data['result_msg'] = $check['message'];
                        $data['return_msg'] = $check['message'];
                    }
                } else {
                    $data['return_code'] = "FAIL";
                    $data['result_code'] = "FAIL";
                    $data['result_msg'] = "数据格式错误";
                    $data['return_msg'] = "请传递正确的数据格式";
                }
            } else {
                $data['return_code'] = "FAIL";
                $data['result_code'] = "FAIL";
                $data['result_msg'] = "数据格式错误";
                $data['return_msg'] = "请传递正确的数据格式";
            }
        } catch (\Exception $e) {
            $data['return_code'] = "FAIL";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误".$e->getMessage() . '-' . $e->getFile() . '-' . $e->getLine();
            $data['return_msg'] = "系统错误" . $e->getMessage();
        }

        trace( "API返回结果" . json_encode($data,256),'SysRunLog');
        return json_encode($data);


    }


    //检查登陆账号代理商是否正常
    private function check_agent($param){
        //所属平台信息
        $Platinfo = new Platinfo();
        $plat_info = $Platinfo->getagentinfo('mid', $param['merchantid']);
        //此代理商是否被停用
        if ($plat_info['agent_id'] == 24 || $plat_info['agent_id'] == 28 || $plat_info['agent_id'] == 32){
//            $res_data['code'] = "FAIL";
            $res_data['code'] = "SUCCESS";
//            $res_data['code'] = 1;
        }else{
            $res_data['code'] = "SUCCESS";
        }
        return $res_data;
    }

    private function checkparam($param, $type)
    {
        switch ($type) {
            case "carduserlist":
                $sdata = array('method', 'merchantid', 'source', 'page', 'search_val','version', 'timestamp', 'signaturenonce');
                break;
            case "cardreportlist":
                $sdata = array('method', 'merchantid', 'source', 'report_type', 'page', 'version', 'timestamp', 'signaturenonce');
                break;
            case "carduserreportlist":
                $sdata = array('method', 'code', 'merchantid', 'source', 'report_type', 'page', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqreportlist":
                $sdata = array('method', 'merchantid', 'page', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "getcardqrcode":
                $sdata = array('method', 'merchantid', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "carduserinfo":
                $sdata = array('method', 'code', 'merchantid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqinfo":       //
                $sdata = array('method', 'code', 'merchantid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqinfotest":
                $sdata = array('method', 'code', 'merchantid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardczgive":
                $sdata = array('method', 'merchantid', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "cardczgiveprice":
                $sdata = array('method', 'merchantid', 'csid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonusrec":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonuscon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqcon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbalancecon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbalancerec":
                $sdata = array('method', 'csid', 'merchantid', 'pay_cate', 'card_code', 'source', 'out_trade_no', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "pay":
                $sdata = array('method', 'merchantid', 'confirm', 'client_ip', 'auth_code', 'source', 'device_id', 'total_fee', 'pay_cate', 'version', 'timestamp', 'signaturenonce');
                break;
            case "orderquery":      //
                $sdata = array('method', 'merchantid', 'out_trade_no', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderquery_app":      //
                $sdata = array('method', 'merchantid', 'out_trade_no', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderrefund":
                $sdata = array('method', 'merchantid', 'out_trade_no', 'device_id', "source", 'refund_fee', 'refund_desc', 'version', 'timestamp', 'signaturenonce');
                break;
            case "orderrefundquery":
                $sdata = array('merchantid', 'refund_no', 'out_trade_no', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderinfo":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderfee":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderreverse":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "paylog":
                $sdata = array('merchantid', 'start_time', 'end_time', 'page', "device_id", 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "paycount":
                $sdata = array('merchantid', 'start_time', 'end_time', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "paystatis":
                $sdata = array('merchantid', 'start_time', 'end_time', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "banjie":
                $sdata = array('merchantid', 'start_time', 'end_time', "device_id", "kuantai", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "clientbinding":
                $sdata = array('binding_code', 'device_code', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "clientbindingque":
                $sdata = array('device_code', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "clientupdata":
                $sdata = array('client_ver', "device_id", 'device_code', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "getprintinfo":
                $sdata = array("print_size", "print_type", "search_key", 'source', "device_id", 'device_code', 'version', 'timestamp', 'signaturenonce');
                break;
            case "login":
                $sdata = array('appid', 'username', 'password', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "machlist":
                $sdata = array('appid', "device_id", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "machinfo":
                $sdata = array('appid', "device_id", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "createorder":
                $sdata = array('appid', "device_id", 'merchantid', 'total_fee', 'pay_cate', 'kuantai', 'channel', 'source', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "createorders":
                $sdata = array('appid', "device_id", 'merchantid', 'total_fee', 'sub_openid', "attach", 'pay_cate', 'client_ip', 'kuantai', 'channel', 'source', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "cardstatis":
                $sdata = array('appid', "device_id", 'start_time', 'end_time', 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "cardqlist":
                $sdata = array('appid', "device_id", 'merchantid', 'source', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "cardqstatis":
                $sdata = array('appid', "device_id", 'start_time', 'end_time', 'cardq_id', 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "getuserlist":
                $sdata = array('appid', "device_id", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "create_wx_order":
                $sdata = array('appid', "device_id", "out_trade_no", 'sub_openid', 'total_fee', 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "getdepositlist":
                $sdata = array('appid', "device_id", 'search', 'status', "page", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "getdepositinfo":
                $sdata = array('appid', "device_id", "search", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "getdeposit_info":
                $sdata = array('appid', "device_id", "orderid", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "depositpay":
                $sdata = array('appid', "device_id", "out_trade_no", "total_fee", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "depositrefund":
                $sdata = array('method', 'merchantid', 'out_trade_no', 'device_id', "source", 'refund_fee', 'refund_desc', 'version', 'timestamp', 'signaturenonce');
                break;
            case "create_czgive":
                $sdata = array('method', 'merchantid', 'version','bonus','limit_sume','card_id','cz_money','zs_money','statu','zs_show', 'timestamp', 'signaturenonce');
                break;
            case "edit_czgive":
                $sdata = array('method', 'merchantid', 'version','id','bonus','limit_sume','card_id','cz_money','zs_money','statu','zs_show', 'timestamp', 'signaturenonce');
                break;
            case "del_czgive":
                $sdata = array('method', 'merchantid', 'version','id', 'timestamp', 'signaturenonce');
                break;
            //创建会员卡
            case "cardus_create":
                $sdata = array('method', 'merchantid', 'param_info', 'card_info',  'version', 'timestamp', 'signaturenonce');
                break;
            //修改会员卡
            case "cardus_edit":
                $sdata = array('method', 'merchantid', 'param_info', 'card_info',  'version', 'timestamp', 'signaturenonce');
                break;
            //删除会员卡
            case "cardus_del":
                $sdata = array('method', 'merchantid', 'card_id', 'version', 'timestamp', 'signaturenonce');
                break;
            //创建优惠券
            case "cards_create":
                $sdata = array('method', 'merchantid',  'param_info', 'card_info', 'version', 'timestamp', 'signaturenonce');
                break;
            //修改优惠券
            case "cards_edit":
                $sdata = array('method', 'merchantid',  'param_info', 'card_info', 'version', 'timestamp', 'signaturenonce');
                break;
            //删除优惠券
            case "cards_del":
                $sdata = array('method', 'merchantid', 'card_id', 'version', 'timestamp', 'signaturenonce');
                break;
            //新增子卡号
            case "rec_user_code":
                $sdata = array('method', 'merchantid', 'card_id','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //新增主卡号
            case "rec_master_code":
                $sdata = array('method', 'merchantid', 'card_id','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //查询子卡号
            case "query_code_info":
                $sdata = array('method', 'merchantid', 'card_id', 'code','version', 'timestamp', 'signaturenonce');
                break;
            //查询主卡号
            case "query_mastercode_info":
                $sdata = array('method', 'merchantid', 'card_id','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //消费权限开关
            case "user_code_shared_off":
                $sdata = array('method', 'merchantid', 'card_id','code','is_shared', 'version', 'timestamp', 'signaturenonce');
                break;
            //同步子卡的积分余额     
            case "user_code_sync":
                $sdata = array('method', 'merchantid', 'card_id','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //同步子卡的积分余额
            case "user_card_code_del":
                $sdata = array('method', 'merchantid', 'card_id','code', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonusrec_ext":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonuscon_ext":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonuscon_not":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
//            case "cardqcon_ext":
//                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'source', 'version', 'timestamp', 'signaturenonce');
//                break;
//            case "cardbalancecon_ext":
//                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
//                break;
            case "cardbalancerec_ext":
                $sdata = array('method', 'csid', 'merchantid', 'pay_cate', 'card_code', 'source', 'out_trade_no', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;

            //员工管理  列表
            case "cashier_list":
                $sdata = array('method', 'merchantid', 'name','version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  添加
            case "cashier_add":
                $sdata = array('method', 'merchantid', 'level','sex','xingming','tel','gonghao','username','password','version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  修改
            case "cashier_edit":
                $sdata = array('method', 'merchantid', 'level','sex','xingming','tel','gonghao','username','password','status', 'version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  删除
            case "cashier_del":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  详情
            case "cashier_info":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  状态
            case "cashier_status":
                $sdata = array('method', 'merchantid', 'id', 'status', 'version', 'timestamp', 'signaturenonce');
                break;
            //员工管理  详情
            case "cashier_role":
                $sdata = array('method', 'merchantid', 'level', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员信息修改
            case "member_edit":
                $sdata = array('method', 'merchantid', 'openid','code','phone','remark','rank','birthday', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员卡券列表
            case "member_cards_list":
                $sdata = array('method', 'merchantid', 'openid', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员次卡列表
            case "member_cardc_list":
                $sdata = array('method', 'merchantid', 'openid', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员积分列表
            case "member_bonus_list":
                $sdata = array('method', 'merchantid', 'openid','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员积分详情
            case "member_bonus_info":
                $sdata = array('method', 'merchantid', 'id','version', 'timestamp', 'signaturenonce');
                break;
            //会员余额列表
            case "member_balance_list":
                $sdata = array('method', 'merchantid', 'openid','code', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员余额详情
            case "member_balance_info":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员余额详情
            case "cards_consume_info":
                $sdata = array('method', 'merchantid', 'search_val', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员余额列表
            case "member_payfee_list":
                $sdata = array('method', 'merchantid','search_val', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员消费列表
            case "member_pay_list":
                $sdata = array('method', 'merchantid', 'openid','version', 'timestamp', 'signaturenonce');
                break;
            //等级列表
            case "cardus_rank_list":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //商户卡券  列表
            case "store_cards_list":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //商户次卡  列表
            case "store_cardc_list":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //商户次卡  购买列表
            case "store_cardc_buylist":
                $sdata = array('method', 'merchantid', 'tims_id', 'version', 'timestamp', 'signaturenonce');
                break;
            //商户次卡  使用列表
            case "store_cardc_uselist":
                $sdata = array('method', 'merchantid', 'tims_id','user_id', 'version', 'timestamp', 'signaturenonce');
                break;
            //云喇叭设置
            case "set_mach_yunlaba":
                $sdata = array('method', 'merchantid', 'ylb_vol','ylb_deviceid','ylb_username', 'version', 'timestamp', 'signaturenonce');
                break;
            //云喇叭详情
            case "info_mach_yunlaba":
                $sdata = array('method', 'merchantid',  'version', 'timestamp', 'signaturenonce');
                break;
            //储值规则列表
            case "czgive_lists":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //储值规则添加
            case "czgive_adds":
                $sdata = array('method', 'merchantid','statu','type',  'bonus','limit_sume','cz_money','zs_money','zs_show','card_arr', 'version', 'timestamp', 'signaturenonce');
                break;
            //储值规则修改
            case "czgive_edits":
                $sdata = array('method', 'merchantid','id','statu', 'bonus','limit_sume','cz_money','zs_money','zs_show','card_arr', 'version', 'timestamp', 'signaturenonce');
                break;
            //储值规则状态
            case "czgive_status":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //储值规则修改
            case "czgive_info":
                $sdata = array('method', 'merchantid','id','version', 'timestamp', 'signaturenonce');
                break;
            //支付后送券  列表
            case "payment_putcards_list":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //支付后送券  添加
            case "payment_putcards_add":
                $sdata = array('method', 'merchantid', 'time_type','times','cardlist','min_fee','max_fee','is_member', 'version', 'timestamp', 'signaturenonce');
                break;
            //支付后送券  状态
            case "payment_putcards_statu":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //支付后送券  删除
            case "payment_putcards_del":
                $sdata = array('method', 'merchantid', 'id', 'version', 'timestamp', 'signaturenonce');
                break;
            //付费卡      设置
            case "cardus_pay_set":
                $sdata = array('method', 'merchantid', 'is_pay','is_pay_balance','price','pay_profiles', 'version', 'timestamp', 'signaturenonce');
                break;
            //付费卡       详情
            case "cardus_pay_info":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //门店二维码       详情
            case "get_mach_qrcode":
                $sdata = array('method', 'merchantid', 'version', 'timestamp', 'signaturenonce');
                break;
            //会员消息群发       方法
            case "send_user_bonusmsg":
                $sdata = array('method', 'merchantid','add_bonus_mass','remark_mass', 'version', 'timestamp', 'signaturenonce');
                break;
            //支付记录   扩展
            case "paylog_extend":
                $sdata = array('merchantid', 'start_time', 'end_time', 'pay_statu', 'd_cashier', 'pay_fs', 'page', "device_id", 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            //支付记录   筛选条件
            case "get_payterms_list":
                $sdata = array('merchantid', "device_id", 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            //获取门店商户号
            case "get_mach_submchid":
                $sdata = array('merchantid', "device_id", 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            /*
            * 代理商的申请及商户、门店的申请
            *
            */
            //申请代理商
            case "apply_agent":
                $sdata = array('method', 'source', 'device_id','version', 'timestamp', 'company_name','realname','mobile','address','open_url','component_appid','component_appsecret','component_encodingaeskey','component_token','requestdomain','wsrequestdomain','uploaddomain','downloaddomain','webviewdomain','wx_pay_appid','wx_pay_appsecret','wx_pay_partnerid','wx_pay_partnerkey','apiclient_key','apiclient_cert','apiclient_rootca','notify_url');
                break;
            //申请商户
            case "apply_momach":
                $sdata = array('method', 'version', 'timestamp', 'company_name','realname','mobile','address','wx_pay_partnerid','agentid','service_charge','momach_type');
                break;
            //添加门店
            case "add_mach":
                $sdata = array('method', 'version', 'timestamp', 'top_machno','merchant','sub_mch_id');
                break;
            //修改门店
            case "edit_mach":
                $sdata = array('method',  'version', 'timestamp', 'signaturenonce','mach_no','merchant','sub_mch_id');
                break;
            //删除门店
            case "del_mach":
                $sdata = array('method', 'version', 'timestamp', 'signaturenonce','mach_no');
                break;
            default:
                $sdata['code'] = "(无)";
                $sdata['message'] = "请求的方法不存在";
                break;

        }

        $arr = array_diff($sdata, $param);
        $arr2 = implode(',', $arr);

        if (!empty($arr2)) {
            $arrs['code'] = "FAIL";
            $arrs['message'] = "缺少参数" . $arr2;
        } else {
            $arrs['code'] = "0";
            $arrs['message'] = "SUCCESS";

        }
        return $arrs;
    }

    public function param_query($param)
    {
        $type = $param['method'];
        switch ($type) {
            case "cardbalancerec":
                if ($param['pay_cate'] == "Cash" || $param['pay_cate'] == "Electronics") {
                    if ($param['pay_cate'] == "Electronics") {
                        if ($param['out_trade_no'] !== "") {
                            $arrs['code'] = "SUCCESS";
                            $arrs['message'] = "SUCCESS";

                        } else {
                            $arrs['code'] = "FAIL";
                            $arrs['message'] = "out_trade_no 参数不能为空";
                        }
                    } else {
                        $arrs['code'] = "SUCCESS";
                        $arrs['message'] = "SUCCESS";
                    }

                } else {
                    $arrs['code'] = "FAIL";
                    $arrs['message'] = "pay_cate 参数不正确";
                }
                break;
        }
        return $arrs;
    }

    public function api_query($array)
    {
        try {

            $type = $array['method'];
            switch ($type) {
                //会员列表
                case 'carduserlist':
                    $data = action('api/cardinfo/carduserlist', array($array));
                    break;
                //会员充值/消费记录
                case "cardreportlist":
                    $data = action('api/cardinfo/cardreportlist', array($array));
                    break;
                //会员卡单用户核销/充值记录
                case "carduserreportlist":
                    $data = action('api/cardinfo/carduserreportlist', array($array));
                    break;
                //卡券核销记录
                case "cardqreportlist":
                    $data = action('api/cardinfo/cardqreportlist', array($array));
                    break;
                //获取会员卡开卡二维码
                case "getcardqrcode":
                    $data = action('api/cardinfo/getcardqrcode', array($array));
                    break;
                //获取会员卡用户信息
                case "carduserinfo":
                    $data = action('api/cardinfo/carduserinfo', array($array));
                    break;
                //获取卡券信息
                case "cardqinfo":
                    $data = action('api/cardinfo/cardqinfo', array($array));
                    break;
                //获取卡券信息  测试
                case "cardqinfotest":
                    $data = action('api/CardThreeInfo/cardqinfotest', array($array));
                    break;
                //获取充送规则
                case "cardczgive":
                    $data = action('api/cardinfo/cardczgive', array($array));
                    break;
                //获取充送规则金额
                case "cardczgiveprice":
                    $data = action('api/cardinfo/cardczgiveprice', array($array));
                    break;
                //会员卡积分充值
                case "cardbonusrec":
                    $data = action('api/cards/cardbonusrec', array($array));
                    break;
                //会员卡积分消费
                case "cardbonuscon":
                    $data = action('api/cards/cardbonuscon', array($array));
                    break;
                //卡券核销
                case "cardqcon":
                    $data = action('api/cards/cardqcon', array($array));
                    break;
                //会员卡余额消费
                case "cardbalancecon":
                    $data = action('api/cards/cardbalancecon', array($array));
                    break;
                //会员卡余额充值
                case "cardbalancerec":
                    $data = action('api/cards/cardbalancerec', array($array));
                    break;
                //支付
                case "pay":
                    $data = action('api/pay/getpay', array($array));
                    break;
                //订单检查
                case "orderquery":
                    $data = action('api/pay/getcheckpay', array($array));
                    break;

                //订单退款
                case "orderrefund":
                    $data = action('api/order/getorderrefund', array($array));
                    break;
                //订单退款查询
                case "orderrefundquery":
                    $data = action('api/order/order_refund_query', array($array));
                    break;
                //订单详情
                case "orderinfo":
                    $data = action('api/order/orderinfo', array($array));
                    break;
                //订单金额
                case "orderfee":
                    $data = action('api/order/getorderprice', array($array));
                    break;
                //撤消订单
                case "orderreverse":
                    $data = action('api/order/getorderreverse', array($array));
                    break;
                //交易记录
                case "paylog":
                    $data = action('api/statis/paylog', array($array));
                    break;
                //交易统计
                case "paycount":
                    $data = action('api/statis/paycount', array($array));
                    break;
                //交易统计（新）
                case "paystatis":
                    $data = action('api/statis/paystatis', array($array));
                    break;
                case "banjie":
                    $data = action('api/statis/banjie', array($array));
                    break;
                //客户端绑定
                case "clientbinding":
                    $data = action('api/login/clientbinding', array($array));
                    break;
                //客户端绑定确认
                case "clientbindingque":
                    $data = action('api/login/clientbindingque', array($array));
                    break;
                case "clientupdata":
                    $data = action('api/login/clientupdata', array($array));
                    break;
                //获取打印信息
                case "getprintinfo":
                    $data = action('api/prints/getprintinfo', array($array));
                    break;
                //用户登录接口
                case "login":
                    $data = action('api/login/login', array($array));
                    break;
                //获取商户列表
                case "machlist":
                    $data = action('api/login/machlist', array($array));
                    break;
                //获取商户信息
                case "machinfo":
                    $data = action('api/login/getmachinfo', array($array));
                    break;
                //创建订单
                case "createorder":
                    $data = action('api/order/create_order', array($array));
                    break;
                //创建订单
                case "createorders":
                    $data = action('api/create_order/create_order', array($array));
                    break;
                //创建微信预付款订单
                case "create_wx_order":
                    $data = action('api/order/create_wx_order', array($array));
                    break;
                //会员卡统计
                case "cardstatis":
                    $data = action('api/statis/cardstatis', array($array));
                    break;
                //获取卡券列表
                case "cardqlist":
                    $data = action('api/cards/cardqlist', array($array));
                    break;
                //获取卡券统计
                case "cardqstatis":
                    $data = action('api/statis/cardqstatis', array($array));
                    break;
                case "getuserlist":
                    $data = action('api/login/getuserlist', array($array));
                    break;
                case "getdepositlist":
                    $data = action('api/DepositOrder/getdepositlist', array($array));
                    break;
                case "getdepositinfo":
                    $data = action('api/DepositOrder/getdepositinfo', array($array));
                    break;
                case "getdeposit_info":
                    $data = action('api/DepositOrder/getdeposit_info', array($array));
                    break;
                case "depositpay":
                    $data = action('api/DepositOrder/wxdepositpay', array($array));
                    break;
                case "depositrefund":
                    $data = action('api/DepositOrder/orderrefund', array($array));
                    break;
                //会员卡创建
                case 'cardus_create':
                    $data = action('api/WxCardus/cardus_create', array($array));
                    break;
                //会员卡修改
                case 'cardus_edit':
                    $data = action('api/WxCardus/cardus_edit', array($array));
                    break;
                //会员卡删除
                case 'cardus_del':
                    $data = action('api/WxCardus/cardus_del', array($array));
                    break;
                //优惠券创建
                case 'cards_create':
                    $data = action('api/WxCardus/cards_create', array($array));
                    break;
                //优惠券修改
                case 'cards_edit':
                    $data = action('api/WxCardus/cards_edit', array($array));
                    break;
                //优惠券删除
                case 'cards_del':
                    $data = action('api/WxCardus/cards_del', array($array));
                    break;
                //新增子卡号
                case "rec_user_code":
                    $data = action('api/WxCardus/rec_user_code', array($array));
                    break;
                //新增主卡号
                case "rec_master_code":
                    $data = action('api/WxCardus/rec_master_code', array($array));
                    break;
                //查询子卡号
                case "query_code_info":
                    $data = action('api/WxCardus/query_code_info', array($array));
                    break;
                //查询主卡号
                case "query_mastercode_info":
                    $data = action('api/WxCardus/query_mastercode_info', array($array));
                    break;
                //消费权限开关
                case "user_code_shared_off":
                    $data = action('api/WxCardus/user_code_shared_off', array($array));
                    break;
                //同步子卡的积分余额
                case "user_code_sync":
                    $data = action('api/WxCardus/user_code_sync', array($array));
                    break;
                //同步子卡的积分余额
                case "user_card_code_del":
                    $data = action('api/WxCardus/user_card_code_del', array($array));
                    break;
                //同步子卡的积分余额
                case "user_code_off":
                    $data = action('api/WxCardus/user_code_off', array($array));
                    break;
                /*
                * 会员卡扩展
                *
                */
                //会员卡积分充值
                case "cardbonusrec_ext":
                    $data = action('api/WxCardusExtend/cardbonusrec_ext', array($array));
                    break;
                //会员卡积分消费
                case "cardbonuscon_ext":
                    $data = action('api/WxCardusExtend/cardbonuscon_ext', array($array));
                    break;
                //会员卡余额消费
                case "cardbalancecon_ext":
                    $data = action('api/WxCardusExtend/cardbalancecon_ext', array($array));
                    break;
                //会员卡余额充值
                case "cardbalancerec_ext":
                    $data = action('api/WxCardusExtend/cardbalancerec_ext', array($array));
                    break;
                //会员卡积分消费
                case "cardbonuscon_not":
                    $data = action('api/WxCardExtend/cardbonuscon', array($array));
                    break;

                /*
                 * 卡券相关
                 *
                 * */
                //创建充送规则
                case "create_czgive":
                    $data = action('api/Cards/create_czgive', array($array));
                    break;
                //修改充送规则
                case "edit_czgive":
                    $data = action('api/Cards/edit_czgive', array($array));
                    break;
                //删除充送规则
                case "del_czgive":
                    $data = action('api/Cards/del_czgive', array($array));
                    break;
                /*
                 *
                 * 2019-10-8 升级接口
                 *
                 */
                //员工管理  列表
                case "cashier_list":
                    $data = action('api/Personnel/cashier_list', array($array));
                    break;
                //员工管理  添加
                case "cashier_add":
                    $data = action('api/Personnel/cashier_add', array($array));
                    break;
                //员工管理  修改
                case "cashier_edit":
                    $data = action('api/Personnel/cashier_edit', array($array));
                    break;
                //员工管理  删除
                case "cashier_del":
                    $data = action('api/Personnel/cashier_del', array($array));
                    break;
                //员工管理  详情
                case "cashier_info":
                    $data = action('api/Personnel/cashier_info', array($array));
                    break;
                //员工管理  状态
                case "cashier_status":
                    $data = action('api/Personnel/cashier_status', array($array));
                    break;
                //员工管理  角色
                case "cashier_role":
                    $data = action('api/Personnel/cashier_role', array($array));
                    break;
                //会员信息修改
                case "member_edit":
                    $data = action('api/CardThreeMember/member_edit', array($array));
                    break;
                //会员卡券列表
                case "member_cards_list":
                    $data = action('api/CardThreeMember/member_cards_list', array($array));
                    break;
                //会员次卡列表
                case "member_cardc_list":
                    $data = action('api/CardThreeMember/member_cardc_list', array($array));
                    break;
                //会员积分列表
                case "member_bonus_list":
                    $data = action('api/CardThreeMember/member_bonus_list', array($array));
                    break;
                //会员积分详情
                case "member_bonus_info":
                    $data = action('api/CardThreeMember/member_bonus_info', array($array));
                    break;
                //会员余额列表
                case "member_balance_list":
                    $data = action('api/CardThreeMember/member_balance_list', array($array));
                    break;
                //会员余额详情
                case "member_balance_info":
                    $data = action('api/CardThreeMember/member_balance_info', array($array));
                    break;
                //核销详情
                case "cards_consume_info":
                    $data = action('api/CardThreeMember/cards_consume_info', array($array));
                    break;
                //会员余额列表  余额
                case "member_payfee_list":
                    $data = action('api/CardThreeMember/member_payfee_list', array($array));
                    break;
                //会员消费列表
                case "member_pay_list":
                    $data = action('api/CardThreeMember/member_pay_list', array($array));
                    break;
                //等级列表
                case "cardus_rank_list":
                    $data = action('api/CardThreeInfo/cardus_rank_list', array($array));
                    break;
                //商户卡券  列表
                case "store_cards_list":
                    $data = action('api/CardThreeInfo/store_cards_list', array($array));
                    break;
                //商户次卡  列表
                case "store_cardc_list":
                    $data = action('api/CardThreeInfo/store_cardc_list', array($array));
                    break;
                //商户次卡  购买列表
                case "store_cardc_buylist":
                    $data = action('api/CardThreeInfo/store_cardc_buylist', array($array));
                    break;
                //商户次卡  使用列表
                case "store_cardc_uselist":
                    $data = action('api/CardThreeInfo/store_cardc_uselist', array($array));
                    break;
                //云喇叭设置
                case "set_mach_yunlaba":
                    $data = action('api/MachThree/set_mach_yunlaba', array($array));
                    break;
                //云喇叭 详情
                case "info_mach_yunlaba":
                    $data = action('api/MachThree/info_mach_yunlaba', array($array));
                    break;
                //门店二维码 详情
                case "get_mach_qrcode":
                    $data = action('api/MachThree/get_mach_qrcode', array($array));
                    break;
                //储值管理  列表
                case "czgive_lists":
                    $data = action('api/CardsThree/czgive_list', array($array));
                    break;
                //储值管理  添加
                case "czgive_adds":
                    $data = action('api/CardsThree/czgive_adds', array($array));
                    break;
                //储值管理  修改
                case "czgive_edits":
                    $data = action('api/CardsThree/czgive_edits', array($array));
                    break;
                //储值管理  状态
                case "czgive_status":
                    $data = action('api/CardsThree/czgive_status', array($array));
                    break;
                //储值规则  详情
                case "czgive_info":
                    $data = action('api/CardsThree/czgive_info', array($array));
                    break;
                //支付后送券  列表
                case "payment_putcards_list":
                    $data = action('api/CardsThree/payment_putcards_list', array($array));
                    break;
                //支付后送券  添加
                case "payment_putcards_add":
                    $data = action('api/CardsThree/payment_putcards_add', array($array));
                    break;
                //支付后送券  状态
                case "payment_putcards_statu":
                    $data = action('api/CardsThree/payment_putcards_statu', array($array));
                    break;
                //支付后送券  删除
                case "payment_putcards_del":
                    $data = action('api/CardsThree/payment_putcards_del', array($array));
                    break;
                //付费卡设置  删除
                case "cardus_pay_set":
                    $data = action('api/CardThreeInfo/cardus_pay_set', array($array));
                    break;
                //付费卡设置  详情
                case "cardus_pay_info":
                    $data = action('api/CardThreeInfo/cardus_pay_info', array($array));
                    break;
                //会员卡群发
                case "send_user_bonusmsg":
                    $data = action('api/CardsThree/send_user_bonusmsg', array($array));
                    break;
                //交易流水      扩展
                case "paylog_extend":
                    $data = action('api/MachThree/paylog_extend', array($array));
                    break;
                //交易流水      筛选条件
                case "get_payterms_list":
                    $data = action('api/MachThree/get_payterms_list', array($array));
                    break;
                //获取门店商户号
                case "get_mach_submchid":
                    $data = action('api/MachThree/get_mach_submchid', array($array));
                    break;
                //订单检查  APP
                case "orderquery_app":
                    $data = action('api/WxAppCreateOrder/wxorderquery', array($array));
                    break;
                /*
                * 代理商的申请及商户、门店的申请
                *
                */
                //申请代理商
                case "apply_agent":
                    $data = action('api/ApplyAgents/apply_agent', array($array));
                    break;
                //申请商户
                case "apply_momach":
                    $data = action('api/ApplyAgents/apply_momach', array($array));
                    break;
                //添加门店
                case "add_mach":
                    $data = action('api/ApplyAgents/add_mach', array($array));
                    break;
                //修改门店
                case "edit_mach":
                    $data = action('api/ApplyAgents/edit_mach', array($array));
                    break;
                //删除门店
                case "del_mach":
                    $data = action('api/ApplyAgents/del_mach', array($array));
                    break;

            }
        } catch (\Exception $e) {
            $data['return_code'] = "SUCCESS";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误".$e->getMessage() . '-' . $e->getFile() . '-' . $e->getLine();
            $data['return_msg'] = "系统错误" . $e->getMessage();
        }
        $data['version'] = "2.0.0";
        $data['timestamp'] = time();
        $data['signaturenonce'] = MD5(rand(1000000, 9999999));
        $data['sign'] = getsign(uni_array($data), "MD5", $array['appid']);
        $logs['type'] = "apilog";
        $logs['up'] = $array;
        $logs['down'] = $data;
        $ActionLogs = new ActionLogs();
        $ActionLogs->writelog("osapi", "clientlog", "log", "client:", $logs);
        return $data;
    }

    //判断appid是否与merchantid匹配
    private function checkappid($appid, $merchantid)
    {
        $map['sign_appid'] = $appid;
        $Agent = new Agent();
        $Agents = $Agent->where($map)->field('id')->select();
        $agentid = [];
        foreach ($Agents as $k => $v) {
            $agentid[] = $v['id'];
        }
        $sid = implode(',', $agentid);
        $mab['agent'] = array('in', $sid);
        $mab['id'] = $merchantid;
        $Merchant = new Merchant();
        $mach = $Merchant->where($mab)->find();
        if ($mach) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }


    }


}